package com.ruoyi.project.web.tXyBill.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.web.tXyBill.domain.TXyBill;
import com.ruoyi.project.web.tXyBill.service.ITXyBillService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;

/**
 * 香烟流水信息操作处理
 * 
 * @author ruoyi
 * @date 2021-07-06
 */
@Controller
@RequestMapping("/web/tXyBill")
public class TXyBillController extends BaseController
{
    private String prefix = "web/tXyBill";
	
	@Autowired
	private ITXyBillService tXyBillService;
	
	@RequiresPermissions("web:tXyBill:view")
	@GetMapping()
	public String tXyBill()
	{
	    return prefix + "/tXyBill";
	}
	
	/**
	 * 查询香烟流水列表
	 */
	@RequiresPermissions("web:tXyBill:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(TXyBill tXyBill)
	{
		startPage();
        List<TXyBill> list = tXyBillService.selectTXyBillList(tXyBill);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出香烟流水列表
	 */
	@RequiresPermissions("web:tXyBill:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TXyBill tXyBill)
    {
    	List<TXyBill> list = tXyBillService.selectTXyBillList(tXyBill);
        ExcelUtil<TXyBill> util = new ExcelUtil<TXyBill>(TXyBill.class);
        return util.exportExcel(list, "tXyBill");
    }
	
	/**
	 * 新增香烟流水
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存香烟流水
	 */
	@RequiresPermissions("web:tXyBill:add")
	@Log(title = "香烟流水", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(TXyBill tXyBill)
	{		
		
		//判断 当前是否存在 存在则更新 否则新增
		TXyBill tXyBill2 = new TXyBill();
		tXyBill2.setRoomBillId(tXyBill.getRoomBillId());
		tXyBill2.setDictCode(tXyBill.getDictCode());
		
		List<TXyBill> tXyBillList =  tXyBillService.selectTXyBillList(tXyBill2);
		if(tXyBillList.size()>0){//如果存在则更新
			tXyBill.setId(tXyBillList.get(0).getId());
			
			if("0".equals(tXyBill.getSl())){
				return toAjax(tXyBillService.deleteTXyBillByIds(tXyBill.getId()));
			}
			
			return toAjax(tXyBillService.updateTXyBill(tXyBill));
		}else{//不存在 新增
			tXyBill.setId(UUID.randomUUID().toString());
			tXyBill.setTxr(ShiroUtils.getSysUser().getUserId()+"");
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
			String formatDate = dateFormat.format(new Date());
			tXyBill.setTxsj(formatDate);
			
			return toAjax(tXyBillService.insertTXyBill(tXyBill));
		}
		
		
	}

	/**
	 * 修改香烟流水
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		TXyBill tXyBill = tXyBillService.selectTXyBillById(id);
		mmap.put("tXyBill", tXyBill);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存香烟流水
	 */
	@RequiresPermissions("web:tXyBill:edit")
	@Log(title = "香烟流水", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(TXyBill tXyBill)
	{		
		return toAjax(tXyBillService.updateTXyBill(tXyBill));
	}
	
	/**
	 * 删除香烟流水
	 */
	@RequiresPermissions("web:tXyBill:remove")
	@Log(title = "香烟流水", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(tXyBillService.deleteTXyBillByIds(ids));
	}
	
}
